package com.ll.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ll.gulimall.product.entity.AttrEntity;
import com.ll.gulimall.product.service.AttrAttrgroupRelationService;
import com.ll.gulimall.product.service.AttrService;
import com.ll.gulimall.product.service.CategoryService;
import com.ll.gulimall.product.vo.AttrAttrGroupRelationVo;
import com.ll.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ll.gulimall.product.entity.AttrGroupEntity;
import com.ll.gulimall.product.service.AttrGroupService;
import com.ll.common.utils.PageUtils;
import com.ll.common.utils.R;


/**
 * 属性分组
 *
 * @author liliang
 * @email liliang@gmail.com
 * @date 2020-07-30 23:16:45
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    @Autowired
    private CategoryService categoryService;

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    //-->/product/attrgroup/{attrgroupId}/attr/relation


    @GetMapping("/{attrgroupId}/attr/relation")
    public R getRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> entitys = attrService.getRelation(attrgroupId);
        return R.ok().put("data", entitys);
    }

    // /product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNotRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page= attrService.getNotRelation(params,attrgroupId);
       return R.ok().put("page",page);
    }


    // /product/attrgroup/attr/relation/delete

    @PostMapping("/attr/relation/delete")
    public R  deleteRelation(@RequestBody  AttrAttrGroupRelationVo[] vos){

        attrService.deleteBatchRelation(vos);
        return R.ok();
    }

    // /product/attrgroup/attr/relation

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody  List<AttrAttrGroupRelationVo> vos){
       relationService .saveBatch(vos);

        return R.ok();

    }

    // /product/attrgroup/{catelogId}/withattr 获取当前分类下的属性分组和属性

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
       List<AttrGroupWithAttrsVo>  vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
